<?php
class StoreProduct extends KF_Auth_Controller{
	/*
        药店列表
    */
    public function index()
    {   
        if(IS_POST)
        {
            $where = array();
            $config = array(
                'table' => 'okdkyao.dk_app_yaodian',
                'columns' => array(
                    array('db' => 'ID'),
                    array('db' => 'Caption')
                ),
                'where' => implode(' and ', $where)
            );
            $this->load->library('dataTables', $config);
            $result = $this->datatables->search();
            
            exit(json_encode($result));
        }
        
        $this->load->view("store_product/index",$res);
    }

    /*
        获取本地药品数量
    */
    public function getProductCount()
    {
        $store_id = $_GET['store_id'];

        $this->load->model("StoreProduct_model", "StoreProductModel");
        $StoreProductModel = $this->StoreProductModel;

        $productCount = $StoreProductModel->getProductCount($store_id);

        $res = array(
            "status" => "success",
            "data" => $productCount
        );
        echo json_encode($res);
    }

    /*
        获取本地药品列表

    */
    public function getProductList()
    {
        if(IS_POST){
            $store_id = $_GET['store_id'];

            $this->load->model("StoreProduct_model", "StoreProductModel");
            $StoreProductModel = $this->StoreProductModel;

            $productList = $StoreProductModel->getProductList($store_id);

            $res = array(
                "status" => "success",
                "data" => $productList
            );

            header('Content-type: application/json');
            echo json_encode($res);
        }else{
            $this->load->view("store_product/product_list", array());
        }
    }

    /*
        克隆药品列表
    */
    public function cloneProduct()
    {
        $source_id = $_GET['source_id'];
        $target_id = $_GET['target_id'];

        $this->load->model("StoreProduct_model", "StoreProductModel");
        $StoreProductModel = $this->StoreProductModel;

        $cloneProduct = $StoreProductModel->cloneProduct($source_id, $target_id);

        if($cloneProduct){
            $res = array(
                "status" => "success",
                "info" => "药品克隆成功"
            );
        }else{
            $res = array(
                "status" => "error",
                "info" => "药品克隆失败"
            );
        }

        header('Content-type: application/json');
        echo json_encode($res);
    }

    /*
        追加药品信息
    */
    public function appendProduct()
    {
        $store_id = $_POST['store_id'];
        $product_id = explode("\n", $_POST['product_id']);

        $product_id = array_unique($product_id);
        foreach ($product_id as $key => $value) {
            if(is_numeric($value) == false){
                unset($product_id[$key]);
            }
        }
        $product_id = array_values($product_id);


        $this->load->model("StoreProduct_model", "StoreProductModel");
        $StoreProductModel = $this->StoreProductModel;

        $appendProduct = $StoreProductModel->appendProduct($store_id, $product_id);

        if($appendProduct){
            $res = array(
                "status" => "success",
                "info" => "药品添加成功"
            );
        }else{
            $res = array(
                "status" => "error",
                "info" => "药品添加失败"
            );
        }

        header('Content-type: application/json');
        echo json_encode($res);
    }

    /*
        删除药品信息
    */
    public function removeProduct()
    {
        $store_id = $_POST['store_id'];
        $product_id = $_POST['product_id'];

        $this->load->model("StoreProduct_model", "StoreProductModel");
        $StoreProductModel = $this->StoreProductModel;

        $removeProduct = $StoreProductModel->removeProduct($store_id, $product_id);


        if($removeProduct){
            $res = array(
                "status" => "success",
                "info" => "药品删除成功"
            );
        }else{
            $res = array(
                "status" => "error",
                "info" => "药品删除失败"
            );
        }

        header('Content-type: application/json');
        echo json_encode($res);
    }
}